package com.chexiehui.jc.mvp.mine;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.CarInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 我的车辆列表
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class MyCarListPresenter extends BasePresenter<MyCarListCovenant.View, MyCarListCovenant.Stores>
        implements MyCarListCovenant.Presenter {

    public MyCarListPresenter(MyCarListCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getList() {
        addSubscription(appStores.getList(""),
                new ApiCallback<BaseModel<List<CarInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<List<CarInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().size() > 0) {
                                mvpView.onGetListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "暂无爱车信息");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void deleteCar(String id, int position) {
        mvpView.showLoading("正在删除");
        addSubscription(appStores.deleteCar(id),
                new ApiCallback<BaseModel<Map<String, Object>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Map<String, Object>> model) {
                        if (model.getResultCode() == 0) {
                            model.setResultInfo("删除成功");
                            Map<String, Object> map = new HashMap<>();
                            map.put("id", id);
                            map.put("position", position);
                            model.setData(map);
                            mvpView.onDeleteCarSuccess(model);
                            mvpView.hide();
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onGetListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}